package com.superzqb.asus.superzqbActivity;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.MineFragment;
import com.superzqb.asus.superzqbFragment.NewsFragment;
import com.superzqb.asus.superzqbFragment.PjobFragment;
import com.superzqb.asus.superzqbFragment.SaveFragment;
import com.superzqb.asus.superzqbFragment.ShopFragment;
import com.superzqb.asus.superzqbFragment.superzqbSave.StagingAssistant;
import com.superzqb.asus.superzqbUtil.GeneralFun;




/**
 * Created by asus on 2017/1/17.
 */
public class Main extends FragmentActivity {
    private SharedPreferences sp;
    private ViewPager mViewPager;
    //适配器
    private FragmentPagerAdapter mAdapter;
    //装载Fragment的集合
    private List<Fragment> mFragments;

    //五个Tab对应的布局
    private LinearLayout mTabPjob;
    private LinearLayout mTabShop;
    private LinearLayout mTabSave;
    private LinearLayout mTabNews;
    private LinearLayout mTabMine;

    //五个Tab对应的ImageButton
    private ImageButton mImgPjob;
    private ImageButton mImgShop;
    private ImageButton mImgSave;
    private ImageButton mImgNews;
    private ImageButton mImgMine;

    //五个Tab对应的TextView
    private TextView mColorPjob;
    private TextView mColorShop;
    private TextView mColorSave;
    private TextView mColorNews;
    private TextView mColorMine;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();//初始化控件
        initEvents();//初始化事件
        initDatas();//初始化数据
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(new StagingAssistant().isFlag()){
            new StagingAssistant().setFlag(false);
            resetImgs();
            resetColor();
            selectTab(1);
        }
    }
    private void initDatas() {
        mFragments = new ArrayList<Fragment>();
        //将五个Fragment加入集合中
        mFragments.add(new PjobFragment());
        mFragments.add(new ShopFragment());
        mFragments.add(new SaveFragment());
        mFragments.add(new NewsFragment());
        mFragments.add(new MineFragment());

        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                // TODO Auto-generated method stub
                return mFragments.get(position);
            }
        };

        //不要忘记设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        mViewPager.addOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                /*判断是否需要登录*/
                if(position==0||position==1){
                    mViewPager.setCurrentItem(position);
                    resetImgs();
                    resetColor();
                    selectTab(position);
                }else{
                    /*判断是否登陆*/
                   /* Toast.makeText(Main.this,sp.getString("uid", "0"),Toast.LENGTH_SHORT).show();*/
                    if (sp.getString("uid","0").equals("0")) {
                        mViewPager.setCurrentItem(1);
                        new GeneralFun().login(Main.this);
                    }else {
                        resetImgs();
                        resetColor();
                        mViewPager.setCurrentItem(position);
                        selectTab(position);
                    }
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    private void initEvents() {
        //设置五个Tab的点击事件
        mTabPjob.setOnClickListener(onClickListener);
        mTabShop.setOnClickListener(onClickListener);
        mTabSave.setOnClickListener(onClickListener);
        mTabNews.setOnClickListener(onClickListener);
        mTabMine.setOnClickListener(onClickListener);

    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //先将五个ImageButton置为灰色
            int i=0;
            if(sp.getString("uid",null)!=null)
            {
                resetImgs();
                resetColor();
                i=1;
            }
            //根据点击的Tab切换不同的页面及设置对应的ImageButton为绿色
            switch (v.getId()) {
                case R.id.id_tab_job:
                    selectTab(0);
                    break;
                case R.id.id_tab_shop:
                    selectTab(1);
                    break;
                case R.id.id_tab_save:
                    if(i==1)
                        selectTab(2);
                    else
                        new GeneralFun().login(Main.this);
                    break;
                case R.id.id_tab_news:
                   if(1==i)
                        selectTab(3);
                    else
                       new GeneralFun().login(Main.this);
                    break;
                case R.id.id_tab_mine:
                    if(1==i)
                    selectTab(4);
                    else
                        new GeneralFun().login(Main.this);
                    break;
                default:
                    break;
            }
        }
    };

    //初始化控件
    private void initViews() {
        sp=getSharedPreferences("uinfo",this.MODE_PRIVATE);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mTabPjob= (LinearLayout) findViewById(R.id.id_tab_job);
        mTabShop= (LinearLayout) findViewById(R.id.id_tab_shop);
        mTabSave = (LinearLayout) findViewById(R.id.id_tab_save);
        mTabNews = (LinearLayout) findViewById(R.id.id_tab_news);
        mTabMine = (LinearLayout) findViewById(R.id.id_tab_mine);

        mImgPjob= (ImageButton) findViewById(R.id.id_tab_job_img);
        mImgShop= (ImageButton) findViewById(R.id.id_tab_shop_img);
        mImgSave = (ImageButton) findViewById(R.id.id_tab_save_img);
        mImgNews = (ImageButton) findViewById(R.id.id_tab_news_img);
        mImgMine = (ImageButton) findViewById(R.id.id_tab_mine_img);

        mColorPjob= (TextView) findViewById(R.id.id_tab_job_color);
        mColorShop= (TextView) findViewById(R.id.id_tab_shop_color);
        mColorSave= (TextView) findViewById(R.id.id_tab_save_color);
        mColorNews= (TextView) findViewById(R.id.id_tab_news_color);
        mColorMine= (TextView) findViewById(R.id.id_tab_mine_color);
    }

    private void selectTab(int i) {
        //根据点击的Tab设置对应的ImageButton为绿色
        switch (i) {
            case 0:
                mImgPjob.setImageResource(R.mipmap.job_pre);
                mColorPjob.setTextColor(0xff1296db);
                break;
            case 1:
                mImgShop.setImageResource(R.mipmap.shop_pre);
                mColorShop.setTextColor(0xff1296db);
                break;
            case 2:
                mImgSave.setImageResource(R.mipmap.money_pre);
                mColorSave.setTextColor(0xff1296db);
                break;
            case 3:
                mImgNews.setImageResource(R.mipmap.news_pre);
                mColorNews.setTextColor(0xff1296db);
                break;
            case 4:
                mImgMine.setImageResource(R.mipmap.mine_pre);
                mColorMine.setTextColor(0xff1296db);
                break;
        }
        //设置当前点击的Tab所对应的页面
        mViewPager.setCurrentItem(i);
    }

    //将五个ImageButton设置为灰色
    private void resetImgs() {
        mImgPjob.setImageResource(R.mipmap.job) ;
        mImgShop.setImageResource(R.mipmap.shop );
        mImgSave.setImageResource(R.mipmap.money);
        mImgNews.setImageResource(R.mipmap.news);
        mImgMine.setImageResource(R.mipmap.mine);
    }
    //将五个字体设置为灰色
    private void resetColor() {
        mColorPjob.setTextColor(0xff8a8a8a);
        mColorShop.setTextColor(0xff8a8a8a);
        mColorSave.setTextColor(0xff8a8a8a);
        mColorNews.setTextColor(0xff8a8a8a);
        mColorMine.setTextColor(0xff8a8a8a);
    }
}
